package com.wingsfrog.puzzle.util;



import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;

/**
 * Created by albert on 16/4/12.
 */
public class SudokuUtil {


    public static int[][] genSudoku(){
        int table[][]=new int[9][9];
        genMiddle(table);
        genOther(table);
        System.out.println("====== finish ======");
        System.out.println(Arrays.toString(table[0]));
        System.out.println(Arrays.toString(table[1]));
        System.out.println(Arrays.toString(table[2]));
        System.out.println(Arrays.toString(table[3]));
        System.out.println(Arrays.toString(table[4]));
        System.out.println(Arrays.toString(table[5]));
        System.out.println(Arrays.toString(table[6]));
        System.out.println(Arrays.toString(table[7]));
        System.out.println(Arrays.toString(table[8]));
        return table;
    }


    public static int[] randomLine(){
        int line[]=new int[9];
        for(int i=0;i<9;i++){
            line[i]=(int) (Math.random() * 9) + 1;
            for(int j=0;j<i;j++){
                if(line[i]==line[j]){
                    i--;
                    break;
                }
            }
        }
        System.out.println("====== randomLine ======");
        System.out.println(Arrays.toString(line));
        return line;
    }

    public static void genMiddle(int[][] table){
        int line[]=randomLine();
        for(int i=3;i<6;i++) {
            for (int j = 3; j < 6; j++) {
                table[i][j] = line[(i - 3) * 3 + j - 3];
            }
        }
    }

    public static void genOther(int[][] table){
        //上下左右九宫格
        for (int i = 3; i < 6; ++i)
        {
            int l = 0;
            for (int j = 3; j < 6; ++j)
            {
                if (i == 3)
                {
                    table[i + 1][l] = table[i][j];
                    table[i + 2][l + 6] = table[i][j];
                    ++l;
                }
                else if (i == 4)
                {
                    table[i + 1][l] = table[i][j];
                    table[i - 1][l + 6] = table[i][j];
                    ++l;
                }
                else if (i == 5)
                {
                    table[i - 2][l] = table[i][j];
                    table[i - 1][l + 6] = table[i][j];
                    ++l;
                }
            }
        }
        for (int j = 3; j < 6; ++j)
        {
            int l = 0;
            for (int i = 3; i < 6; ++i)
            {
                if (j == 3)
                {
                    table[l][j + 1] = table[i][j];
                    table[l + 6][j + 2] = table[i][j];
                    ++l;
                }
                else if (j == 4)
                {
                    table[l][j + 1] = table[i][j];
                    table[l + 6][j - 1] = table[i][j];
                    ++l;
                }
                else if (j == 5)
                {
                    table[l][j - 2] = table[i][j];
                    table[l + 6][j - 1] = table[i][j];
                    ++l;
                }
            }
        }

        //角落上四个九宫格
        for (int i = 0; i < 3; ++i)
        {
            int l = 0;
            for (int j = 3; j < 6; ++j)
            {
                if (i == 0)
                {
                    table[i + 1][l] = table[i][j];
                    table[i + 2][l + 6] = table[i][j];
                    ++l;
                }
                else if (i == 1)
                {
                    table[i + 1][l] = table[i][j];
                    table[i - 1][l + 6] = table[i][j];
                    ++l;
                }
                else if (i == 2)
                {
                    table[i - 2][l] = table[i][j];
                    table[i - 1][l + 6] = table[i][j];
                    ++l;
                }
            }
        }
        for (int i = 6; i < 9; ++i)
        {
            int l = 0;
            for (int j = 3; j < 6; ++j)
            {
                if (i == 6)
                {
                    table[i + 1][l] = table[i][j];
                    table[i + 2][l + 6] = table[i][j];
                    ++l;
                }
                else if (i == 7)
                {
                    table[i + 1][l] = table[i][j];
                    table[i - 1][l + 6] = table[i][j];
                    ++l;
                }
                else if (i == 8)
                {
                    table[i - 2][l] = table[i][j];
                    table[i - 1][l + 6] = table[i][j];
                    ++l;
                }
            }
        }

    }







//    public static void main(String[] args) {
//        int[][] table=SudokuUtil.genSudoku();
//    }
}

